package service;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import entity.Post;
import mapper.PostMapper;
import mapper.UserMapper;

public class PostServiceImpl implements PostService {
	 @Autowired
	    private PostMapper postMapper;

	    @Override
	    @Transactional
	    public boolean createPost(Post post) {
	        // 补全默认值
	        post.setCreateTime(LocalDateTime.now());
	        post.setUpdateTime(LocalDateTime.now());
	        post.setLikeCount(0);
	        post.setCommentCount(0);
	        post.setStatus(0); // 0-正常状态
	        return postMapper.insert(post) > 0;
	    }

	    @Override
	    @Transactional
	    public boolean deletePost(Long postId, Long operatorId) {
	    	 Post post = postMapper.selectById(postId);
	         if (post == null || !post.getUserId().equals(listPostsByUserId(operatorId, 0))) {
	             return false; // 无权限删除
	         }
	         return postMapper.delete(postId) > 0;
	     }
	  

	    @Override
	    @Transactional
	    public boolean updatePost(Post post) {
	        // 1. 校验作者权限
	        Post oldPost = postMapper.selectById(post.getId());
	        if (oldPost == null || !oldPost.getUserId().equals(post.getUserId())) {
	            return false;
	        }
	        // 2. 更新内容和时间
	        post.setUpdateTime(LocalDateTime.now());
	        return postMapper.update(post) > 0;
	    }

	    @Override
	    public Post getPostById(Long postId) {
	        return postMapper.selectById(postId);
	    }

	    @Override
	    public List<Post> listPosts(int pageNum, int pageSize, int sortType) {
	        int offset = (pageNum - 1) * pageSize;
	        // 排序字段：0-按create_time降序（最新），1-按like_count降序（最热）
	        String orderBy = sortType == 0 ? "create_time DESC" : "like_count DESC";
	        return postMapper.selectByPage(offset, pageSize, orderBy);
	    }

	    @Override
	    public List<Post> listPostsByUserId(Long userId, int pageNum) {
	        int offset = (pageNum - 1) * 10; // 固定每页10条
	        return postMapper.selectByUserId(userId, offset, 10);
	    }

	    @Override
	    @Transactional
	    public void updateLikeCount(Long postId) {
	        // 查询当前点赞数并+1（实际应从like表统计，此处简化）
	        Post post = postMapper.selectById(postId);
	        if (post != null) {
	            post.setLikeCount(post.getLikeCount() + 1);
	            postMapper.update(post);
	        }
	    }

	    @Override
	    @Transactional
	    public void updateCommentCount(Long postId) {
	        // 类似点赞数更新逻辑
	        Post post = postMapper.selectById(postId);
	        if (post != null) {
	            post.setCommentCount(post.getCommentCount() + 1);
	            postMapper.update(post);
	        }
	    }

	
		@Override
		public Object getAllPosts(int i, int j) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void deletePost(Integer id) {
			// TODO Auto-generated method stub
			
		}


}
